#include <dbg.h>
#include <gtest/gtest.h>

using std::string;
using std::unordered_map;
using std::vector;

class Solution {
public:
    static auto twoSum(const vector<int>& nums, const int target)
        -> vector<int> {
        unordered_map<int, int> hashMap;
        for (int i = 0; i < nums.size(); ++i) {
            int complement = target - nums[i];  // 计算目标值与当前元素的差值
            dbg(complement);

            if (auto it = hashMap.find(complement);
                it != hashMap.end()) {  // 查找差值是否已经存在于映射表中
                return {it->second,
                        i};  // 如果存在，则返回差值的索引和当前元素的索引
            }

            hashMap[nums[i]] = i;  // 将当前元素及其索引存入映射表中
            dbg(hashMap);
        }
        return {};
    }
};

TEST(TEST1_twoSum_Test, twoSum) {
    Solution s;

    constexpr int target = 9;
    const vector nums{2, 7, 11, 15};
    EXPECT_EQ(s.twoSum(nums, target), vector({0, 1}));
}

TEST(TEST2_twoSum_Test, twoSum) {
    Solution s;

    constexpr int target = 6;
    const vector nums{3, 2, 4};
    EXPECT_EQ(s.twoSum(nums, target), vector({1, 2}));
}

TEST(TEST3_twoSum_Test, twoSum) {
    Solution s;

    constexpr int target = 6;
    const vector nums{3, 3};
    EXPECT_EQ(s.twoSum(nums, target), vector({0, 1}));
}

int main(int argc, char** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}